# -*- coding: utf-8 -*-
# Created by liangfuting on 2020/6/17
# Copyright (c) 2020 liangfuting. All rights reserved.

from django import forms
from django.core.exceptions import ValidationError
from rbac.models import UserInfo

class UserInfoModelForm(forms.ModelForm):
    confirm_password=forms.CharField(label='确认密码',max_length=256,required=True)

    class Meta:
        model=UserInfo
        fields=['name','email','password','confirm_password']
        #自定义错误信息
        # error_messages={
        #     'name':{'required':'用户名不能为空'},
        #     'email': {'required': '邮箱不能为空'},
        #     'password': {'required': '密码不能为空'},
        #     'confirm_password': {'required': '密码不能为空'},
        # }

    def __init__(self,*args,**kwargs):
        super(UserInfoModelForm,self).__init__(*args,**kwargs)
        for name,field in self.fields.items():
            field.widget.attrs['class']='form-control'
            field.widget.attrs['placeholder']=field.label

    def clean_confirm_password(self):
        #钩子方法
        password=self.cleaned_data['password']
        confirm_password=self.cleaned_data['confirm_password']
        #密码不一致抛出异常

        if password != confirm_password:
            # print(password, confirm_password)
            raise ValidationError("两次输入密码不一致")
        # 密码一样的话，返回确认后的密码
        return confirm_password

class UpdateUserInfoModelForm(forms.ModelForm):

    class Meta:
        model=UserInfo
        fields=['name','email']
        #自定义错误信息
        # error_messages={
        #     'name':{'required':'用户名不能为空'},
        #     'email': {'required': '邮箱不能为空'},
        #     'password': {'required': '密码不能为空'},
        #     'confirm_password': {'required': '密码不能为空'},
        # }

    def __init__(self,*args,**kwargs):
        super(UpdateUserInfoModelForm,self).__init__(*args,**kwargs)
        for name,field in self.fields.items():
            field.widget.attrs['class']='form-control'
            field.widget.attrs['placeholder']=field.label


class ResetPasswordModelForm(forms.ModelForm):
    confirm_password = forms.CharField(label='确认密码', max_length=256, required=True)

    class Meta:
        model=UserInfo
        fields=['password','confirm_password']
        #自定义错误信息
        # error_messages={
        #     'name':{'required':'用户名不能为空'},
        #     'email': {'required': '邮箱不能为空'},
        #     'password': {'required': '密码不能为空'},
        #     'confirm_password': {'required': '密码不能为空'},
        # }

    def __init__(self,*args,**kwargs):
        super(ResetPasswordModelForm,self).__init__(*args,**kwargs)
        for name,field in self.fields.items():
            field.widget.attrs['class']='form-control'
            field.widget.attrs['placeholder']=field.label

    def clean_confirm_password(self):
        password = self.cleaned_data['password']
        confirm_password = self.cleaned_data['confirm_password']
        # 密码不一致抛出异常
        # print(password,confirm_password)
        if password != confirm_password:
            # print(password, confirm_password)
            #抛出异常一定要用raise ,之前用的return 结果怎么都无法弹出提示
            raise ValidationError("两次输入密码不一致")
        # 密码一样的话，返回确认后的密码

        return confirm_password
